package com.tencent.View;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.tencent.FaceTrackFaceu.R;


/**
 * Created by Zhou HongChun on 2017/7/7.
 *
 */

public class MyAdapter extends RecyclerView.Adapter {
    private final Context mContext;
    private final int[] mImageviews;
    int nexPosition = 0;
    int loopTimes;
    int lineNum = 1;
    private ClickEvent mClickEvent;

    public MyAdapter(Context context, int[] imageviews) {
        mContext = context;
        mImageviews = imageviews;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_view2, parent, false);
        return new MyViewHolder2(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder2 myViewHolder2 = (MyViewHolder2) holder;
        myViewHolder2.iv.setImageResource(mImageviews[position]);

        myViewHolder2.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Log.e("answer","position:   " + position );
                mClickEvent.clickEvent(position);
            }
        });

    }


    @Override
    public int getItemCount() {
        return mImageviews.length;
    }

    private class MyViewHolder2 extends RecyclerView.ViewHolder{

        private final ImageView iv;

        public MyViewHolder2(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.view2_iv);
        }
    }

    public interface ClickEvent{
        void clickEvent(int position);

    }

    public void getClickEvent(ClickEvent clickEvent){
        mClickEvent = clickEvent;
    }

/*    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_view, parent, false);
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        lineNum++;
        if ((mImageviews.length % 4 == 0 ? mImageviews.length / 4 : mImageviews.length / 4 + 1) < lineNum - 1) {   //会出现指针越界情况，由于时间紧迫，先简单处理，日后有时间再细细解决
            return;
        }
        calculateLoopTimes(holder, position);

        for (int i = 0; i < loopTimes; i++) {
            //位于一行中的哪个位置
            int which_position ;
            //数组中的图片位置
            int img_position;

            //如果数组长度为4的整数就把nexPosition减4再一个一个加1
            if (mImageviews.length % 4 != 0 && nexPosition == mImageviews.length) {
                img_position = nexPosition - mImageviews.length % 4 + i;
            } else {
                img_position = nexPosition - 4 + i;
            }
            which_position = (img_position) % 4;

            Log.e("answer","which_position:   " + which_position + "     img_position:  " +img_position );
            switch (which_position) {
                case 0:
                    myViewHolder.imageView1.setImageResource(mImageviews[img_position]);
                    break;
                case 1:
                    myViewHolder.imageView2.setImageResource(mImageviews[img_position]);
                    break;
                case 2:
                    myViewHolder.imageView3.setImageResource(mImageviews[img_position]);
                    break;
                case 3:
                    myViewHolder.imageView4.setImageResource(mImageviews[img_position]);
                    break;
            }
        }

    }

    private void calculateLoopTimes(RecyclerView.ViewHolder holder, int position) {

        myViewHolder = (MyViewHolder) holder;

        setClikEvent(myViewHolder, position);

        Log.e("answer", "几行几行几行：      "+(lineNum) + "　　getItemCount中算出的行数：" +  (mImageviews.length % 4 == 0 ? mImageviews.length / 4 : mImageviews.length / 4 + 1));
        Log.e("answer","mImageviews.length % 4: " + mImageviews.length % 4+"      nexPosition:  " + nexPosition + "      " + "mImageviews.length - mImageviews.length % 4:  " + (mImageviews.length - mImageviews.length % 4) + "");

        if (mImageviews.length % 4 != 0 && nexPosition == mImageviews.length - mImageviews.length % 4) {   //数组长度不为4的倍数
            nexPosition = mImageviews.length;
            loopTimes = mImageviews.length % 4;
        } else {
            nexPosition = nexPosition + 4;
            loopTimes = 4;   //for循环的次数为4次
        }
        Log.e("answer","nextpositon----------next------:  " +nexPosition + "    mImageviews.length" + mImageviews.length);
    }

    private void setClikEvent(final MyViewHolder myViewHolder, final int position) {
        myViewHolder.imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mClickEvent.clickEvent_1();
                int id = myViewHolder.imageView1.getId();
                Log.e("id_image","id-----------------------:  " +String.valueOf(position));
            }
        });
        myViewHolder.imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = myViewHolder.imageView2.getId();
                Log.e("id_image","id-----------------------:  " +String.valueOf(position));
//                mClickEvent.clickEvent_2();
            }
        });
        myViewHolder.imageView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = myViewHolder.imageView3.getId();
                Log.e("id_image","id-----------------------:  " +String.valueOf(position));
//                mClickEvent.clickEvent_3();
            }
        });
        myViewHolder.imageView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = myViewHolder.imageView4.getId();
                Log.e("id_image","id-----------------------:  " +String.valueOf(position));
//                mClickEvent.clickEvent_4();
            }
        });
    }


    @Override
    public int getItemCount() {
        return mImageviews.length % 4 == 0 ? mImageviews.length / 4 : mImageviews.length / 4 + 1;
    }

    //这个继承RecyclerView.ViewHolder的类千万记得要写
    private class MyViewHolder extends RecyclerView.ViewHolder{

        public ImageView imageView1;
        private ImageView imageView2;
        private ImageView imageView3;
        private ImageView imageView4;

        private MyViewHolder(View itemView) {
            super(itemView);
            imageView1 = (ImageView) itemView.findViewById(R.id.left_1);
            imageView2 = (ImageView) itemView.findViewById(R.id.left_2);
            imageView3 = (ImageView) itemView.findViewById(R.id.left_3);
            imageView4 = (ImageView) itemView.findViewById(R.id.left_4);
        }
    }

    public interface ClickEvent{
        void clickEvent_1();

        void clickEvent_2();

        void clickEvent_3();

        void clickEvent_4();
    }

    public void getClickEvent(ClickEvent clickEvent){
        mClickEvent = clickEvent;
    }*/
}
